<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 8.1.&nbsp; MySQL Database</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-8.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-8-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-8-SECT-1"></a>
<h3 id="title-IDAG21EF" class="docSection1Title">8.1. MySQL Database</h3>
<p class="docText">MySQL has its own client interface, allowing you to move data around and change database configuration. Note that you must use a password to log in. Assigning database <span class="docEmphasis">users</span> allows you to limit access to server tables that have multiple users. Each MySQL server, where tables are grouped together, can host many databases. Normally, a web application has its own proprietary database.</p>
<p class="docText">You may have installed MySQL yourself or have access through your ISP. Most ISPs that support PHP also provide a MySQL database for your use. Should you have difficulty, check their support pages or contact them to determine connection details. You'll need to know the following:</p>
<ul><li><p class="docList">The IP address of the database server</p></li><li><p class="docList">The name of the database</p></li><li><p class="docList">The username</P></li><li><p class="docList">The password</P></LI></ul>
<p class="docText">If you've installed MySQL on your computer, you'll be able to use the defaults from the installation and the password you specified. This chapter looks at two ways to communicate with MySQL, the command line<a name="IDX-CHP-8-0354"></a> 
 and phpMyAdmin, a web-based tool.</P>
<a name="learnphpmysql-CHP-8-SECT-1.1"></a>
<h4 id="title-IDAL31EF" class="docSection2Title">8.1.1. Accessing the Database with the Command Line</h4>
<p class="docText">One way of communicating with MySQL is via the MySQL Command Line Client.<a name="IDX-CHP-8-0355"></a> 
 Depending on which operating system you're using, you either need to open a command shell for Windows (type <tt>cmd</tt> from the Run dialog, as shown in <a class="docLink" href="#learnphpmysql-CHP-8-FIG-1">Figure 8-1</a>) or open a terminal session, in Mac OS X and Unix environments.</p>
<a name="learnphpmysql-CHP-8-FIG-1"></a><P><center>
<h5 class="docFigureTitle">Figure 8-1. Windows Run dialog</h5>
<img border="0" alt="" width="347" height="179" SRC="images/learnphpmysql_0801.jpg">
</center></P><br>
<p class="docText">Once you reach the command line, type <tt>mysql</tt> and press Enter. The syntax for the <tt>mysql</tt> command is:</p>
<pre>
mysql -h <tt><I>hostname</i></tt> -u <tt><i>user</i></tt> -p
</pre><br>

<p class="docText">If you've installed MySQL on your computer, the default username is <tt>root</tt>. You can omit the hostname flag and value. Enter your password when MySQL displays the "Enter password" prompt.<a name="IDX-CHP-8-0356"></a> 
 If the password, username, and hostname are correct, you'll see a banner message like that in <a class="docLink" href="#learnphpmysql-CHP-8-FIG-2">Figure 8-2</a>.</P>
<a name="learnphpmysql-CHP-8-FIG-2"></a><P><center>
<h5 class="docFigureTitle">Figure 8-2. A successful login to MySQL</h5>
<img border="0" alt="" width="549" height="146" SRC="images/learnphpmysql_0802.jpg">
</center></p><br>
<p class="docText">Don't let the MySQL command-line interface alarm you; it's not difficult to use.</P>
<a name="learnphpmysql-CHP-8-SECT-1.1.1"></a>
<h5 id="title-IDAX51EF" class="docSection3Title">8.1.1.1. Prompts</H5>
<a name="IDX-CHP-8-0357"></a> 

<p class="docText">At the MySQL prompt, you can enter database commands followed by Enter. There is also a set of commands that MySQL itself interprets. For a list of these commands, type <tt>help</tt> or <tt>\h</tt> at the <tt>mysql&gt;</tt> prompt. <a class="docLink" href="#learnphpmysql-CHP-8-TABLE-1">Table 8-1</a> shows some of the prompts you'll see and summarizes what they mean.</p>
<a name="learnphpmysql-CHP-8-TABLE-1"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><H5 class="docTableTitle">Table 8-1. Command prompt meanings</H5></caption><colgroup span="2"><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Prompt</p></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</p></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText"><tt>mysql&gt;</tt></p></td><td class="docTableCell" align="left"><p class="docText">Waiting for a command</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>-&gt;</tt></P></td><td class="docTableCell" align="left"><p class="docText">Waiting for the next line of a command</P></TD></tr><TR><td class="docTableCell" align="left"><p class="docText"><tt>'&gt;</tt></p></td><TD class="docTableCell" align="left"><p class="docText">Waiting for the next line of a string that starts with a single quote</p></td></TR><tr><td class="docTableCell" align="left"><p class="docText"><tt>"&gt;</tt></P></td><td class="docTableCell" align="left"><p class="docText">Waiting for the next line of a string that starts with a double quote</p></td></TR></table></P><br>

<a name="learnphpmysql-CHP-8-SECT-1.1.2"></a>
<h5 id="title-IDAJD2EF" class="docSection3Title">8.1.1.2. Commands</h5>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-8-TABLE-2">Table 8-2</a> lists commands<a name="IDX-CHP-8-0358"></a> 
 that are available at the MySQL<a name="IDX-CHP-8-0359"></a> 
 prompt.</p>
<a name="learnphpmysql-CHP-8-TABLE-2"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 8-2. MySQL client commands</H5></caption><colgroup span="3"><col><col><col></colgroup><thead><tr><th class="thead" scope="col" align="left"><p class="docText">Command</P></th><th class="thead" scope="col" align="left"><p class="docText">Parameter</P></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</P></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText"><tt>quit</tt></p></td><td class="docTableCell">&nbsp;</td><td class="docTableCell" align="left"><p class="docText">Exit the command-line utility</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>use</tt></p></TD><td class="docTableCell" align="left"><p class="docText">Database name</p></TD><TD class="docTableCell" align="left"><p class="docText">Use a specific database</p></TD></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>show</tt></P></td><td class="docTableCell" align="left"><p class="docText"><tt>tables</tt> or <tt>databases</tt></P></td><td class="docTableCell" align="left"><p class="docText">Show lists such as tables or databases available</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>describe</tt></P></TD><td class="docTableCell" align="left"><p class="docText">Table name</p></td><td class="docTableCell" align="left"><p class="docText">Describe a table's columns</P></td></TR><tr><TD class="docTableCell" align="left"><p class="docText"><tt>status</tt></P></TD><td class="docTableCell">&nbsp;</td><td class="docTableCell" align="left"><p class="docText">Display database version and status</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText"><tt>source</tt></p></td><td class="docTableCell" align="left"><p class="docText">Filename</p></td><td class="docTableCell" align="left"><p class="docText">Execute commands from a file as a script</P></td></tr></table></P><BR>
<p class="docText">These commands allow you to perform tasks such as executing SQL commands that are stored in a script file using the <tt>source</tt>.</p>
<p class="docText">To display the available databases, type:</P>
<pre>
mysql&gt; SHOW DATABASES;
</pre><br>

<p class="docText">which returns:</p>
<pre>
+----------+
| Database |
+----------+
| mysql    |
+----------+
1 rows in set (0.00 sec)
</pre><br>

<P><table border="0" bgcolor="black" cellspacing="0" cellpadding="1" width="90%" align="center"><tr><td><table bgcolor="white" width="100%" border="0" cellspacing="0" cellpadding="6"><TR><td width="60" valign="top"><img src="images/tip_yellow.jpg" width="50" height="54" alt=""></td><TD valign="top">
<p class="docText">To scroll back though commands you've already entered in MySQL, use the up arrow key.</p>
</td></tr></table></td></TR></table></P><br>
<p class="docText">The default database that is present after an install is called <tt>mysql</tt>. The <tt>mysql</tt> database also stores the database user authentication information. Don't delete it! When you started <tt>mysql</tt>, you didn't specify connection to a particular database. The <tt>use</tt> command allows you to do this.</p>
<p class="docText">To connect to the <tt>mysql</tt> database, type the following at the MySQL prompt:</p>
<pre>
USE `mysql`;
</pre><br>

<p class="docText">This returns:</P>
<pre>
Database changed
</pre><br>

<p class="docText">If your ISP supplied a different database name, use that instead of <tt>mysql</tt>.</P>



<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-8.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-8-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
